package com.ddstation.crud.service;

import com.ddstation.app.holiday.service.HolidayService;
import com.ddstation.app.sewage.model.DtInvsAddDisinfectantHistoryModel;
import com.ddstation.crud.entity.DtInvsAddDisinfectantEntity;
import com.ddstation.crud.entity.DtInvsIllegalCheckDailyEntity;
import com.ddstation.crud.mapper.DtInvsAddDisinfectantMapper;
import com.ddstation.crud.mapper.DtInvsIllegalCheckDailyMapper;
import com.ddstation.home_page.service.DtInvsMessageQueueService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.inject.Inject;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;

@Service
public class DtInvsAddDisinfectantServiceImpl implements DtInvsAddDisinfectantService {

    @Inject private DtInvsAddDisinfectantMapper dtInvsAddDisinfectantMapper;
    @Inject private DtInvsMessageQueueService dtInvsMessageQueueService;
    @Inject private DtInvsIllegalCheckDailyMapper dtInvsIllegalCheckDailyMapper;
    @Inject private HolidayService holidayService;

    @Override
    @Transactional(rollbackFor=Exception.class)
    public boolean insert(DtInvsAddDisinfectantEntity entity) {

        dtInvsAddDisinfectantMapper.insert(entity);
        //新增消息
        dtInvsMessageQueueService.insertMessageQueue(entity.getInstitutionId(),302,1,1,entity.getId().toString(),1);

        // 更新卫监日常记录
        Date today = new Date();
        List<DtInvsIllegalCheckDailyEntity> checkList = dtInvsIllegalCheckDailyMapper.selectOneByDateAndInstitution(today, entity.getInstitutionId());
        LocalDateTime thisInstant = LocalDateTime.ofInstant(today.toInstant(), ZoneId.systemDefault());
        LocalDateTime noon = LocalDateTime.of(thisInstant.toLocalDate(), LocalTime.of(12, 0, 0,0));
        if (null != checkList && !checkList.isEmpty()) {
            for (DtInvsIllegalCheckDailyEntity ele: checkList) {
                if (ele.getBusinessSubType() == 302) {
                    if (thisInstant.isAfter(noon) && ele.getAmPm() != null && ele.getAmPm() == 1) {
                        ele.setTargetId(entity.getId().toString());//取最近一次登记的记录
                        dtInvsIllegalCheckDailyMapper.updateByPrimaryKeySelective(ele);
                        return entity.getId() > 0;
                    }
                    if (thisInstant.isBefore(noon) && ele.getAmPm() != null && ele.getAmPm() == 0) {
                        ele.setTargetId(entity.getId().toString());//取最近一次登记的记录
                        dtInvsIllegalCheckDailyMapper.updateByPrimaryKeySelective(ele);
                        return entity.getId() > 0;
                    }
                }
            }
        }
        DtInvsIllegalCheckDailyEntity _checkRet1 = new DtInvsIllegalCheckDailyEntity();// 一条check结果
        _checkRet1.setInstitutionId( entity.getInstitutionId());//机构ID
        _checkRet1.setCheckDt(today);//检查日期
        _checkRet1.setBusinessType(30);//'@10：消毒登记 @20：医废登记 @30：检测审查 @40：处方违规',
        _checkRet1.setBusinessSubType(302);//'子分类 @302：余氯

        String dateStr = DateTimeFormatter.ofPattern("yyyy-MM-dd").format(thisInstant);
        if (holidayService.checkHoliday(dateStr, entity.getInstitutionId())) {
            _checkRet1.setMsgId("900007");
            _checkRet1.setMessage("休息日");
        }
        _checkRet1.setCheckSt(0);//正常
        _checkRet1.setTargetId(entity.getId().toString());//取最近一次登记的记录

        if (entity.getCheckTm().toInstant().isAfter(noon.atZone(ZoneId.systemDefault()).toInstant())) {
            _checkRet1.setAmPm(1);
        } else {
            _checkRet1.setAmPm(0);
        }
        dtInvsIllegalCheckDailyMapper.insert(_checkRet1);

		return entity.getId()>0;
	}

    @Override
    public List<DtInvsAddDisinfectantHistoryModel> selectByHistory(String institutionId, int pageNo, int pageSize) {
        int startNo = (pageNo - 1) * pageSize;
        return dtInvsAddDisinfectantMapper.selectByHistory(institutionId,startNo,pageSize);
    }

    @Override
    public DtInvsAddDisinfectantEntity selectById(Integer id) {
        return dtInvsAddDisinfectantMapper.selectByPrimaryKey(id);
    }

}
